//
//  TrackPolyline.m
//  2bulu-NewAssistant
//
//  Created by Kent Peifeng Ke on 3/19/15.
//  Copyright (c) 2015 È≠èÊñ∞Êù∞. All rights reserved.
//

#import "TrackPolyline.h"
#import "LAArrowPolylineRender.h"
#import "LAArrowPolylineRender.h"

static CGFloat sLineStrokeWidth = 4.0;

@implementation TrackPolyline
{
    @protected
    __weak LAArrowPolylineRender *_renderer;
}


+(void)setDefaultPolylineStrokeWidth:(CGFloat)w
{
    sLineStrokeWidth = w;
}

-(instancetype)init
{
    self = [super init];
    self.lineStrokeWidth = sLineStrokeWidth;
#if TARGET_OS_IPHONE
    self.markFont = [UIFont systemFontOfSize:12];
#else
    self.markFont = [NSFont systemFontOfSize:12];
#endif
    self.rendererClass = [LAArrowPolylineRender class];
    return self;
}

-(void)setupRenderer:(LAArrowPolylineRender *)renderer
{
    [super setupRenderer:renderer];
    renderer.reverseArrow = self.reverse;
    renderer.startPointMarkString = self.startPointMarkText;
    renderer.endPointMarkString = self.endPointMarkText;
    renderer.markStringFont = self.markFont;
}


@end

// llf 2016.12.24 created
#pragma mark-
@implementation ValueAddedTrackPolyLine
{
    float *_pValue;
    BOOL setValues;
    
//    UIColor *_minColor;
//    UIColor *_midColor;
//    UIColor *_maxColor;
//    
    float _minRefValue;//最小参考值 对应于_minColor的颜色
    float _midRefValue;//中间参考值 对应于_midColor的颜色
    float _maxRefValue;//最大参考值 对应于_maxColor的颜色
    
}


+ (instancetype)polylineWithPoints:(const MKMapPoint *)points PointsValues:(const float *)values count:(NSUInteger)count
{
    
    ValueAddedTrackPolyLine *obj = [self polylineWithPoints:points count:count];
    [obj setPointsValue:values];
    [obj setDefaultColors];
    [obj setDefaultRefrenceValues];
    return obj;
}
+ (instancetype)polylineWithCoordinates:(const CLLocationCoordinate2D *)coords PointsValues:(const float *)values count:(NSUInteger)count
{
    ValueAddedTrackPolyLine *obj = [self polylineWithCoordinates:coords count:count];
    [obj setPointsValue:values];
    [obj setDefaultColors];
    [obj setDefaultRefrenceValues];
    return obj;
}


-(float *)pointsValues
{
    return _pValue;
}

-(void)setDefaultColors
{
#if TARGET_OS_IPHONE
    _minColor = [UIColor greenColor];
    _midColor = [UIColor yellowColor];
    _maxColor = [UIColor redColor];
#else
    _minColor = [NSColor greenColor];
    _midColor = [NSColor yellowColor];
    _maxColor = [NSColor redColor];
#endif
}

-(void)setDefaultRefrenceValues
{
    if(!_pValue)
        return;
    
    float tmpMaxRef = FLT_MIN;
    float tmpMinRef = FLT_MAX;
  //  CGFloat tmpSum = CGFLOAT_MIN;
    NSUInteger c = self.pointCount;
    for(NSUInteger i=0; i<c; i++)
    {
        float v = _pValue[i];
        if(v > tmpMaxRef)
            tmpMaxRef = v;
        if(v < tmpMinRef)
            tmpMinRef = v;
        
      //  tmpSum += v;
    }
    
    _minRefValue = tmpMinRef;
    _maxRefValue = tmpMaxRef;
    _midRefValue = (_maxRefValue+_minRefValue)/2;//tmpSum/c;//
    
    
}

-(void)setPointsValue:(const float *)value
{
    if(_pValue)
    {
        free(_pValue);
        _pValue = NULL;
    }
    
    if(value)
    {
        _pValue = calloc(self.pointCount, sizeof(float));
        memcpy(_pValue, value, self.pointCount*sizeof(float));
    }
    
}

-(void)setupRenderer:(LAArrowPolylineRender *)renderer
{
    [super setupRenderer:renderer];
    GradientsColorCompnent *pColors = calloc(self.pointCount, sizeof(GradientsColorCompnent));
    NSUInteger colorCount = self.pointCount;
    BOOL success = [self createColorValuesToBuffer:pColors];
    if(success)
        [renderer setPointsColors:pColors count:colorCount];
    else
        [renderer setPointsColors:NULL count:0];
    
    free(pColors);
}



//设定最小、中间、最大三个颜色色值 这个方法必须在对象创建完后立即调用才有效
#if TARGET_OS_IPHONE
-(void)setMinValueColor:(nonnull UIColor *)minColor midValueColor:(nonnull UIColor *)midColor maxValueColor:(nonnull UIColor *)maxColor
#else
-(void)setMinValueColor:(nonnull NSColor *)minColor midValueColor:(nonnull NSColor *)midColor maxValueColor:(nonnull NSColor *)maxColor
#endif
{
    _minColor = [minColor copy];
    _midColor = [midColor copy];
    _maxColor = [maxColor copy];
    
}

//设定最小参考值、中间参考值、最大参考值，如果不设定的话，最小参考值就是pointsValues的最小值，中间参考值就是pointsValues的中间值，最大参考值就是pointsValues的最大值
-(void)setMinValueReference:(float)minRef midValueReference:(float)midRef maxValueReference:(float)maxRef
{
    _minRefValue = minRef;
    _midRefValue = midRef;
    _maxRefValue = maxRef;
}


//将每个点对应的颜色在写到colorsValueBuffer中 colorsValueBuffer的元素长度要和轨迹点数目对应\
成功返回YES,失败返回NO
-(BOOL)createColorValuesToBuffer:(GradientsColorCompnent *)colorsValueBuffer
{
    if(!_pValue || !colorsValueBuffer)
        return NO;
    
    if(fabs(_midRefValue - _maxRefValue)<0.0000001)
        return NO;
    
    GradientsColorCompnent minCpn = [[self class] colorCompnetFromColor:_minColor];
    GradientsColorCompnent midCpn = [[self class] colorCompnetFromColor:_midColor];
    GradientsColorCompnent maxCpn = [[self class] colorCompnetFromColor:_maxColor];
    
    GradientsColorCompnent *pColors = colorsValueBuffer;
    
    float delta1 = _midRefValue - _minRefValue;//中间参考和最小参考之间的差值
    float delta2 = _maxRefValue - _midRefValue;//最大参考和中间参考的差值
//    NSLog(@"--_maxRefValue:%.2f _midRefValue:%.2f _minRefValue:%.2f",_maxRefValue,_midRefValue,_minRefValue);
//    NSLog(@"minCpn r:%.2f g:%.2f b:%.2f a:%.2f",minCpn.r,minCpn.g,minCpn.b,minCpn.a);
//    NSLog(@"midCpn r:%.2f g:%.2f b:%.2f a:%.2f",midCpn.r,midCpn.g,midCpn.b,midCpn.a);
//    NSLog(@"maxCpn r:%.2f g:%.2f b:%.2f a:%.2f",maxCpn.r,maxCpn.g,maxCpn.b,maxCpn.a);
    
    GradientsColorCompnent tmpCpn;
    NSUInteger ct = self.pointCount;
    for(NSUInteger i=0; i<ct; i++)
    {
        float v = _pValue[i];
        if(v <= _minRefValue)//小于最小参考值
        {
            tmpCpn = minCpn;
        }
        else if(v <= _midRefValue)//介于最小参考和中介参考之间
        {
            float percent = (v - _minRefValue)/delta1;
            
            tmpCpn.r = (midCpn.r - minCpn.r)*percent + minCpn.r;
            tmpCpn.g = (midCpn.g - minCpn.g)*percent + minCpn.g;
            tmpCpn.b = (midCpn.b - minCpn.b)*percent + minCpn.b;
            tmpCpn.a = (midCpn.a - minCpn.a)*percent + minCpn.a;
        }
        else if(v < _maxRefValue)//介于中间参考和最大参考值之间
        {
            float percent = (v - _midRefValue)/delta2;
            tmpCpn.r = (maxCpn.r - midCpn.r)*percent + midCpn.r;
            tmpCpn.g = (maxCpn.g - midCpn.g)*percent + midCpn.g;
            tmpCpn.b = (maxCpn.b - midCpn.b)*percent + midCpn.b;
            tmpCpn.a = (maxCpn.a - midCpn.a)*percent + midCpn.a;
            
//            printf("i:%lu  v:%.2f   r:%.2f g:%.2f b:%.2f a:%.2f --percent:%.3f\n",i,v,tmpCpn.r,tmpCpn.g,tmpCpn.b,tmpCpn.a,percent);
        }
        else//大于等于最大参考
        {
            tmpCpn = maxCpn;
        }
        
        pColors[i] = tmpCpn;
        
        
    }

    
    return YES;
}
#if TARGET_OS_IPHONE
+(GradientsColorCompnent)colorCompnetFromColor:(nonnull UIColor *)color
#else
+(GradientsColorCompnent)colorCompnetFromColor:(nonnull NSColor *)color
#endif
{
    GradientsColorCompnent component;
    CGFloat r,g,b,a;
    [color getRed:&r green:&g blue:&b alpha:&a];
    component.r = r;
    component.g = g;
    component.b = b;
    component.a = a;
    return component;
}


-(void)dealloc
{
    if(_pValue)
    {
        free(_pValue);
    }
}




@end
